Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow passing a filterset class to the filterset factory #1636

Conversation

b1rger
Copy link
Contributor

@b1rger b1rger commented Jan 19, 2024

I've tried to reuse the variable name terminology from django.forms.models.modelform_factory

Closes: #1631

b1rger added a commit to acdh-oeaw/apis-core-rdf that referenced this pull request Jan 19, 2024
The custom implementation did not allow to use custom Meta attributes,
therefore we are backporting the one we proposed on
carltongibson/django-filter#1636
b1rger added a commit to acdh-oeaw/apis-core-rdf that referenced this pull request Jan 19, 2024
The custom implementation did not allow to use custom Meta attributes,
therefore we are backporting the one we proposed on
carltongibson/django-filter#1636
Copy link
Owner

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @b1rger — this looks good.

Could you add a test or two using the new argument? Specifically, interested in the base filter set defining some properties to be reused, fields and meta options: inheritance of meta options needs testing. Also we should doc this.

@b1rger
Copy link
Contributor Author

b1rger commented Jan 24, 2024

Could you add a test or two using the new argument? Specifically, interested in the base filter set defining some properties to be reused, fields and meta options: inheritance of meta options needs testing. Also we should doc this.

done

@b1rger
Copy link
Contributor Author

b1rger commented Jan 25, 2024

FTR: currently the Meta.fields attribute of the base filterset is always overwritten by the fields that is passed to the factory (which defaults to ALL_FIELDS) - that means if you want to provide use a custom filterset class together with custom field definitions, you would have to both provide a custom filtersetclass and pass the fields to the factory. It might be more usable if the fields argument of the factory got only used if the base filterset Meta class does not have one. Or if the fields argument of the factory is None, but that would break the method signature... maybe only set fields if fields is None and not hasattr(filterset.Meta, "fields")...

@carltongibson
Copy link
Owner

It's probably worth comparing how modelform_factory does it.

@b1rger
Copy link
Contributor Author

b1rger commented Jan 25, 2024

It's probably worth comparing how modelform_factory does it.

They are passing fields=None to the factory and then:

    attrs = {"model": model}
    if fields is not None:
        attrs["fields"] = fields

I can update the code accordingly, if its ok to change the fields argument default from ALL_FIELDS to None

@carltongibson
Copy link
Owner

carltongibson commented Jan 25, 2024

They're subclassing the Meta class, so that defined options are inherited.

But yes, as long as we maintain the effective existing behaviour where fields wasn't passed, with no base FormSet class, then we should be able to adjust.

@b1rger b1rger force-pushed the birger/1631-filterset_factory-pass-class branch from 4fed365 to a594860 Compare January 25, 2024 19:18
@codecov-commenter
Copy link

codecov-commenter commented Jan 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (af9f13b) 98.59% compared to head (b590408) 98.59%.
Report is 2 commits behind head on main.

❗ Current head b590408 differs from pull request most recent head 75f0cf2. Consider uploading reports for the commit 75f0cf2 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1636   +/-   ##
=======================================
  Coverage   98.59%   98.59%           
=======================================
  Files          15       15           
  Lines        1283     1284    +1     
=======================================
+ Hits         1265     1266    +1     
  Misses         18       18           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@b1rger
Copy link
Contributor Author

b1rger commented Jan 25, 2024

ack, the Meta.fields in the passed filterset now trumps the fields

@b1rger
Copy link
Contributor Author

b1rger commented Jan 25, 2024

ack, the Meta.fields in the passed filterset now trumps the fields

Hm, scratch that, i pushed too fast, i'll have to rethink the logic...

@b1rger b1rger force-pushed the birger/1631-filterset_factory-pass-class branch from a594860 to 75f0cf2 Compare January 27, 2024 14:44
@b1rger
Copy link
Contributor Author

b1rger commented Jan 27, 2024

Oke, I think I found an approach that should not lead to surprises: if the fields argument is not None it overrides everything. If it is None, the Meta.fields of the filterset argument is used, if it exists and is not None. If thats also not the case, fields is set to ALL_FIELDS

Copy link
Owner

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think this looks good. Just the one comment.

result, params = filterset.filter_for_lookup(f, "isnull")
self.assertEqual(result, BooleanFilter)

def test_filtesret_factory_base_filter_meta_inheritance_exclude(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check the typos in the test method names please? (Same with test above too.)

@carltongibson
Copy link
Owner

Updated in #1644

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow passing a filterset class to filterset_factory
3 participants